<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-1-19 10:16:50
 * @Description: 用于服务项目操作
 */
class ScsServiceController extends BaseController
{
    /**
     * @Author:      zwh
     * @DateTime:    2016-1-19 10:17:49
     * @Description: 基础产品包列表
     */
    public function service_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            // $ppc=ProductPackageCity::where("city_id",$sc_user->shop->city_id)->where("status",1)->select("pack_id")->get()->toArray();
            $ppc=ProductPackageShop::getShopPackage($sc_user->shop_id);
            $pp=ProductPackage::whereIn("id",$ppc)->where('status',1)->where('place',2)->select('id','name','desc')->orderBy("sort","ASC")->get();
            $temp_arr=array("count"=>sizeof($pp),"product_package"=>$pp);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "service", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-15 10:22:38
     * @Description: 匹配车型的配件分类列表
     */
    public function parts_list(){
        $validator = Validator::make(Input::all(),
            array(
                'token'        => 'required',
                'car_model_id' => 'required|max:20|alpha_dash',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $spi_arr=DB::table("supply_product_item")
            ->leftJoin("supply_item","supply_item.id","=","supply_product_item.supply_item_id")
            ->leftJoin("supply_subject","supply_subject.id","=","supply_item.subject")
            // ->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","supply_product_item.car_model_id")
            // ->leftJoin("car_series_type","supply_product_item.car_series_id","=","car_series_type.id")
            // ->leftJoin("car_brand_type","car_brand_type.id","=","supply_product_item.car_brand_id")
            ->groupBy("supply_subject.id")
            ->where("supply_item.status",1)
            ->where("supply_product_item.car_model_id",Input::get('car_model_id'))
            ->select("supply_subject.id as ss_id","supply_subject.name as ss_name","supply_subject.desc as ss_desc")->get();
            $temp_arr=array("count"=>sizeof($spi_arr),"supply_product_item"=>$spi_arr);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "parts_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-15 13:42:38
     * @Description: 匹配当前车型耗材信息
     */
    //疑似废弃 暂时注释
    // public function supply_list(){
    //     $validator = Validator::make(Input::all(),
    //         array(
    //             'token'        => 'required',
    //             'car_model_id' => 'required|max:20|alpha_dash',
    //             'subject_id'   => 'required'
    //         )
    //         );
    //     $response = array('code' => 0, 'msg' => 'ok');
    //     do{
    //         if($validator->fails()) {
    //             $response = Lang::get("scs_error.args_error_10000");
    //             $response["result"]=$validator->messages()->all();
    //             break;
    //         }
    //         $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
    //         if(is_null($sc_user)){
    //             break;
    //         }
    //         $spi=DB::table("supply_product_item")
    //         ->leftJoin("supply_item","supply_item.id","=","supply_product_item.supply_item_id")
    //         ->leftJoin("supply_subject","supply_subject.id","=","supply_item.subject")
    //         ->leftJoin("anhui_data.car_standard_info","anhui_data.car_standard_info.id","=","supply_product_item.car_model_id")
    //         // ->leftJoin("anhui_data.car_series","supply_product_item.car_series_id","=","anhui_data.car_series.id")
    //         // ->leftJoin("anhui_data.car_brand","supply_product_item.car_brand_id","=","anhui_data.car_brand.id")
    //         ->leftJoin("supply_brand","supply_brand.id","=","supply_item.brand_id")
    //         ->leftJoin("work_model_coefficient","work_model_coefficient.name","=","anhui_data.car_standard_info.model_level")
    //         ->where("supply_item.status",1)
    //         ->where("supply_brand.status",1)
    //         ->where("supply_product_item.car_model_id",Input::get('car_model_id'))
    //         ->where("supply_product_item.status",1)
    //         ->where("supply_item.subject",Input::get('subject_id'))
    //         ->groupBy("supply_item.id");
    //         //品牌
    //         if(Input::get('brand_id') && is_numeric(Input::get('brand_id'))){
    //             $spi->where('supply_product_item.car_brand_id',Input::get('car_brand_id'));
    //         }

    //         //型号
    //         if(trim(Input::get('model'))){
    //             $spi->where('supply_item.model','like',"%".trim(Input::get('model'))."%");
    //         }

    //         //数量
    //         if(Input::get('num')){

    //         }

    //         $spi_arr=$spi->select("supply_item.id","model","amount","purchase_price","supply_subject.id as ss_id","supply_brand.brand_name","work_model_coefficient.supply_coefficient")->get();
    //         $temp_arr=array("count"=>sizeof($spi_arr),"supply_item"=>$spi_arr);
    //         $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
    //     }while(false);
    //     // Log::info("info", array("system" => "scs" ,"event" => "supply_list", 'input' =>Input::all() ,"output" => $response));
    //     return json_encode($response);
    // }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-28 18:01:52
     * @Description: 匹配当前车型耗材信息
     */
    //疑似废弃 暂时注释
    public function supply_lists(){
        $validator = Validator::make(Input::all(),
            array(
                'token'        => 'required',
                'car_model_id' => 'required|max:20|alpha_dash',
                'supply_item_id'   => 'required'
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $si=SupplyItem::find(Input::get('supply_item_id'));
            if(!$si || $si->status!=1){
                $response = Lang::get("scs_error.supply_item_error_50005");
                break;
            }
            $spi=DB::table("anhui_data.supply_product_item")
            ->leftJoin("supply_item","supply_item.id","=","supply_product_item.supply_item_id")
            ->leftJoin("supply_classification","supply_classification.id","=","supply_item.supply_classification_id")
            ->leftJoin("anhui_data.car_standard_info","car_standard_info.id","=","supply_product_item.car_model_id")
            ->leftJoin(DB::raw("(select * from anhui.supply_sub where `status`=1) as supply_sub"), 'supply_sub.id',"=","supply_item.id")
            // ->leftJoin("anhui_data.car_series","supply_product_item.car_series_id","=","anhui_data.car_series.id")
            // ->leftJoin("car_brand_type","supply_product_item.car_brand_id","=","car_brand_type.id")
            ->leftJoin("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")
            ->leftJoin("work_model_coefficient","work_model_coefficient.name","=","anhui_data.car_standard_info.model_level")
            ->where("supply_item.status",1)
            ->where("supply_brand.status",1)
            ->where("supply_product_item.car_model_id",Input::get('car_model_id'))
            ->where("supply_product_item.status",1)
            ->where("supply_item.supply_classification_id",$si->supply_classification_id)
            ->groupBy("supply_item.id");
            //品牌
            if(Input::get('brand_id') && is_numeric(Input::get('brand_id'))){
                $spi->where('supply_product_item.car_brand_id',Input::get('car_brand_id'));
            }

            //型号
            if(trim(Input::get('model'))){
                $spi->where('supply_item.model','like',"%".trim(Input::get('model'))."%");
            }

            //数量
            if(Input::get('num')){

            }

            $spi_arr=$spi->select("supply_item.id","model","supply_sub.usual_price as amount","supply_sub.usual_price as purchase_price","supply_classification.id as ss_id","supply_brand.brand_name","work_model_coefficient.supply_coefficient")->get();
            $temp_arr=array("count"=>sizeof($spi_arr),"supply_item"=>$spi_arr);
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $temp_arr);
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "supply_list", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-02-24 11:01:25
     * @Description: 用于获取 耗材的资源
     * 包含耗材的 品牌
     *包含耗材的 分类
     *
     */
    // public function supplyServices(){
    //     $validator = Validator::make(Input::all(),
    //         array(
    //             'token'        => 'required',
    //         )
    //     );
    //     $response = array('code' => 0, 'msg' => 'ok');
    //     do{
    //         if($validator->fails()) {
    //             $response = Lang::get("scs_error.args_error_10000");
    //             $response["result"]=$validator->messages()->all();
    //             break;
    //         }
    //         $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
    //         if(is_null($sc_user)){
    //             break;
    //         }
    //         $ss=SupplySubject::where("status",1)->get();
    //         $sb=SupplyBrand::where("status",1)->get();

    //         $response = array('code' => 0, 'msg' => 'ok', 'result' => array("supply_subject"=>$ss,"supply_brand"=>$sb));
    //     }while(false);
    //     // Log::info("info", array("system" => "scs" ,"event" => "supplyServices", 'input' =>Input::all() ,"output" => $response));
    //     return json_encode($response);

    // }

    /**
     * @Description   根据供应商Id获取（当前门店的）结算方式、物流配送
     * @return string
     * @author yyc
     * @date 2018/9/13 11:15
     */
    public function payTaxFreight(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "supplierId"=>'integer|required',
            )
        );

        do {
            //组织全局返回信息
            $response = ['code' => 0, 'msg' => 'ok'];

            //验证参数
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"] = $validator->messages()->all();
                break;
            }

            //验证用户信息
            $sc_user = $this->verificationSCSUserToken(Input::get("token"), $response);
            if(is_null($sc_user)){
                break;
            }

            //验证并获取供应商信息（是否配置当前的门店信息）
            $supplier = MaintenanceSupplier::find(Input::get('supplierId'));
            if (is_null($supplier)) {
                $response["result"] = Lang::get("scs_error.supplier_error_10013");
                break;
            }

            //当不是全局的时候查询信息
            $result = MaintenanceSupplier::getPayTaxFreight(Input::get('supplierId'), $sc_user->shop_id, $response, $sc_user->shop->city_id);
            if (is_null($result)) {
                break;
            }

            $response['result'] = $result;

        } while (false);

        return json_encode($response);

    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-02-24 19:12:49
     * @Description: 用于门店的供应商的查询接口
     */
    public function supplierList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "page"=>'integer',
                "page_size"=>'integer',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ms=MaintenanceSupplier::select("id","name","bm","major_type","car_type","purchase_type","purchase_status","contacts","contacts_number", "regular_pay");
            if(Input::get("purchase_type")!=""){
                $ms=$ms->where("purchase_type",Input::get("purchase_type"));
            }
            if(Input::get("name")!=""){
                $ms=$ms->where('name','like',"%".trim(Input::get('name'))."%");
            }
            if(Input::get("bm")!=""){
                $ms=$ms->where('bm','like',"%".trim(Input::get('bm'))."%");
            }
            if(Input::get("car_type")!=""){
                $ms=$ms->where('car_type','like',"%".trim(Input::get('car_type'))."%");
            }
            if(Input::get("purchase_status")!=""){
                $ms=$ms->where('purchase_status',Input::get('purchase_status'));
            }
            // if(Input::get("name")){
            //     // $ms=$ms->where("name","like",'%'.Input::get("name").'%');
            //     $ms=$ms->where(function($query)
            //     {
            //         $query->where("name","like",'%'.Input::get("name").'%')
            //               ->orWhere("bm","like",'%'.Input::get("name").'%');
            //     });
            // }
            $count=$ms->count();
            $ms=$ms->skip($page_size*($page-1))->take($page_size)->get();
            // $ms=$ms->get();
            
            //是否能挂账
            foreach ($ms as $_ms) {
                $_ms->is_bill=1;//暂时都记做能挂账
                // $cc=DB::table("supplier_accounts")->where("supplier_id",$_ms->id)->where("status",1)->first();
                // $_ms->is_bill=isset($cc)?1:0;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"ms_info"=>$ms));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "supplierList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2017-12-06T12:53:56+0800
     * @return   [type]                   [description]
     * 用于供货商的详情
     */
    public function supplierDetail(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                "id"=>'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ms=MaintenanceSupplier::find(Input::get("id"));      
            if(is_null($ms)){
                $response = Lang::get("scs_error.supplier_error_10013");
                break;
            }      
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("ms_info"=>$ms));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "supplierList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-03-04 11:11:55
     * @Description: 用于查询  门店的信息
     */
    public  function shopList(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $ms=MaintenanceShops::orderBy("created_at","desc");
            if(Input::get("shop_type")){
                $ms=$ms->where("shop_type",Input::get("shop_type"));
            }
            if(Input::get("status")!=""){
                $ms=$ms->where("status",Input::get("status"));
            }
            $ms=$ms->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($ms),"shop_info"=>$ms));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "shopList", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
    
    /**
     * @Author:      zwh
     * @DateTime:    2016-11-23 11:16:34
     * @Description: 部门信息
     */
    public  function department(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $tmp=array();
            $me=MaintenanceEngineers::where('type','shop')->where('type_id',$sc_user->shop_id)->where('status',1)->select('id','name','team')->get();
            $sc=SCUser::where('shop_id',$sc_user->shop_id)->where('status',1)->select('id','real_name','team')->get();
            $tmp['sc']=$sc;
            $tmp['me']=$me;
            $dt=Department::orderBy("created_at","desc");
            if(Input::get("status")!=""){
                $dt=$dt->where("status",Input::get("status"));
            }
            $dt=$dt->get();
            $tmp['count']=sizeof($dt);
            $tmp['department']=$dt;
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $tmp);
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "department", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

   /**
    * @Author:      lyt
    * @DateTime:    2016-02-25 14:54:09
    * @Description: 用于查询 三级的 分类
    * 支持门店的添加 耗材的修改
    */
    // public function statisticalClassification(){

    //     $validator = Validator::make(Input::all(),
    //         array(
    //             'token' => 'required',
    //         )
    //     );
    //     $response = array('code' => 0, 'msg' => 'ok');
    //     do{
    //         if($validator->fails()) {
    //             $response = Lang::get("scs_error.args_error_10000");
    //             $response["result"]=$validator->messages()->all();
    //             break;
    //         }
    //         $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
    //         if(is_null($sc_user)){
    //             break;
    //         }
    //         $db_c=DB::select("SELECT sc.name sc_name,sc.id sc_id,sc2.* FROM supply_classification sc
    //                 LEFT JOIN supply_classification sc2
    //                 on sc.parent_id=sc2.id
    //                 where sc.level=3");
    //         $fina_arr=array();
    //         foreach ($db_c as $_sc) {
    //             if(isset($fina_arr[$_sc->sc_name])){
    //                 $fina_arr[$_sc->sc_name]["item"][]=array("last_id"=>$_sc->sc_id,"id"=>$_sc->id,"name"=>$_sc->name);
    //             }else{
    //                 $fina_arr[$_sc->sc_name]=array("name"=>$_sc->sc_name,"item"=>array(array("last_id"=>$_sc->sc_id,"id"=>$_sc->id,"name"=>$_sc->name)));
    //             }
    //         }
    //         $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($fina_arr),"ms_info"=>array_values($fina_arr)));
    //     }while(false);
    //     // Log::info("info", array("system" => "scs" ,"event" => "statisticalClassification", 'input' =>Input::all() ,"output" => $response));
    //     return json_encode($response);
    // }
    /**
     * @Author:      zwh
     * @DateTime:    2016-4-6 16:15:17
     * @Description: 用于查询  三级的 分类
     * 支持门店的添加 耗材的修改
     * @link http://api.anhui.com/v1/scs/service/statclassifs?token=0c100bf4ccf528951d2e81ffb8a4243e
     */
    public function statisticalClassifications(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $db_c=DB::select("SELECT sc.name sc_name,sc.id sc_id,sc2.* FROM supply_classification sc
                    LEFT JOIN supply_classification sc2
                    on sc.parent_id=sc2.id
                    where sc.level=3 and sc.status=1 and sc2.status=1");
            $fina_arr=array();
            foreach ($db_c as $_sc) {
                if(isset($fina_arr[$_sc->name])){
                    $fina_arr[$_sc->name]["item"][]=array("last_id"=>$_sc->sc_id,"id"=>$_sc->id,"name"=>$_sc->sc_name);
                }else{
                    $fina_arr[$_sc->name]=array("name"=>$_sc->name,"item"=>array(array("last_id"=>$_sc->sc_id,"id"=>$_sc->id,"name"=>$_sc->sc_name)));
                }
                $fina_arr[$_sc->name]['id'] = $_sc->id;
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>sizeof($fina_arr),"ms_info"=>array_values($fina_arr)));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "statisticalClassifications", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author:      lyt
     * @DateTime:    2016-06-08 11:59:01
     * @Description: Description
     */
    public function st(){
        $validator = Validator::make(Input::all(),
            array(
                    'token' => 'required',                    
                )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $st=SupplyClassification::where("level",1)->where("status",1)->get();            
            foreach ($st as $_st) {
                $temp_st=SupplyClassification::where("parent_id",$_st->id)->where("status",1)->get(); 
                foreach ($temp_st as $_temp_st) {
                    $temp_st2=SupplyClassification::where("parent_id",$_temp_st->id)->where("status",1)->get(); 
                    $_temp_st->item=$temp_st2;
                }                
                $_st->item=$temp_st;
            }
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("ms_info"=>$st));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "st", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }    

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2017-12-10T10:39:24+0800
     * @return   [type]                   [description]
     * 用于维护首字母
     */
    public function initials(){
        $validator = Validator::make(Input::all(),
            array(
                    'token' => 'required',    
                    'name' => 'required',                 
                )
            );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }            
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("name"=>Pinyin::getShortPinyin(Input::get("name"))));
        }while(false);
        // Log::info("info", array("system" => "scs" ,"event" => "st", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);

    }
}